package com.ki.erp.util;

import cn.idev.excel.ExcelWriter;
import cn.idev.excel.FastExcel;
import cn.idev.excel.read.listener.PageReadListener;
import cn.idev.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson2.JSON;
import com.ki.erp.model.DemoData;
import com.ki.erp.model.Employee;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExcelUtil {
    public static void main(String[] args) {
        read("simpleWrite1737031489552.xlsx");
    }

    public static void read(String fileName) {
        FastExcel.read(fileName, Employee.class, new PageReadListener<Employee>(dataList -> {
            for (Employee e : dataList) {
                System.out.println("读取到一条数据: " + JSON.toJSONString(e));
            }
        })).sheet().doRead();
    }

    public static void write() {
        String fileName = "simpleWrite" + System.currentTimeMillis() + ".xlsx";

        // 写法2：直接传递数据列表
        FastExcel.write(fileName, DemoData.class)
                .sheet("模板")
                .doWrite(data());


    }

    public static List<DemoData> data() {

        ArrayList<DemoData> demoDataArrayList = new ArrayList<>();

        DemoData demoData = new DemoData();
        demoData.setDoubleData(1.0);
        demoData.setString("1");
        demoData.setDate(new Date());
        demoDataArrayList.add(demoData);

        DemoData demoData2 = new DemoData();
        demoData2.setDoubleData(2.0);
        demoData2.setString("2");
        demoData2.setDate(new Date());
        demoDataArrayList.add(demoData2);

        DemoData demoData3 = new DemoData();
        demoData3.setDoubleData(2.0);
        demoData3.setString("2");
        demoData3.setDate(new Date());
        demoDataArrayList.add(demoData3);


        return demoDataArrayList;
    }

    public void temp() {
        try (ExcelWriter excelWriter = FastExcel.write("fileName", DemoData.class).build()) {
            WriteSheet writeSheet = FastExcel.writerSheet("模板").build();
            for (int i = 0; i < 5; i++) {
                excelWriter.write(data(), writeSheet);
            }
        }

    }
}
